package dbinit;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import core.product.model.RatingAndReview;
import core.util.HibernateCoreUtil;

public class InitRatingAndReview 
{
	public static RatingAndReview getRatingAndReviewByUuid(Long uuid)
	{
		RatingAndReview rar = null ;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try 
		{
				transaction = session.beginTransaction();
				rar = (RatingAndReview) session.get(RatingAndReview.class, uuid);
				transaction.commit();
		}
		catch (HibernateException e) 
		{
			// TODO: handle exception
			transaction.rollback();
			e.printStackTrace();
		}
		finally 
		{
			session.close();
		}
		return rar ;
	}
	public static Long doinit(String review, int rating, Long reviewer, Long p)
	{
		Transaction transaction = null;
		Session session = HibernateCoreUtil.getSessionFactory().openSession();
		RatingAndReview ratingAndReview = new RatingAndReview();
		ratingAndReview.setRating(rating);
		ratingAndReview.setReview(review);
		ratingAndReview.setProduct(InitProduct.getProductByUuid(p));
		ratingAndReview.setReviewer(InitBasicProfile.getBasicProfileByUuid(reviewer));
		Long uuid = null;
		try
		{
			transaction = session.beginTransaction();
		    uuid = (Long)session.save(ratingAndReview);
			transaction.commit();
		}
		catch(HibernateException he)
		{
			he.printStackTrace();
		}
		return uuid ;
	}
}
